package com.fengye.algorithms.huawei;

import java.util.Scanner;

/**
 * https://www.nowcoder.com/practice/181a1a71c7574266ad07f9739f791506?tpId=37&tqId=21288&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
 * @Author fengye
 * @Date 2023/3/11 17:37
 **/
public class SamLongestSubStringMain {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) {
            String aStr = in.nextLine();
            String bStr = in.nextLine();
            // 保证aStr是短的
            if (aStr.length() > bStr.length()) {
                String temp = aStr;
                aStr = bStr;
                bStr = temp;
            }

            if (bStr.contains(aStr)) {
                System.out.println(aStr);
                continue;
            }
            boolean isFind = false;
            for (int i = aStr.length() - 1; i > 0; i--) {
                for (int j = 0; j <= aStr.length() - i; j++) {
                    String sub = aStr.substring(j, j + i);
                    if (bStr.contains(sub)) {
                        System.out.println(sub);
                        isFind = true;
                        break;
                    }
                }
                if (isFind) {
                    break;
                }
            }
        }
    }

    /**
     * https://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b?tpId=37&tqId=21298&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
     * @param args
     */
    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String a = in.next();
            String b = in.next();
            // 保证a.length > b.length
            if(a.length() < b.length()) {
                String t = a;
                a = b;
                b = t;
            }

            if(a.contains(b)) {
                System.out.println(b.length());
                return;
            }

            int max = 0;
            for(int i = 0; i < b.length();i++) {
                for(int j=b.length()-1;j>i;j--) {
                    String sub = b.substring(i,j);
                    if (a.contains(sub) && sub.length() > max) {
                        max = sub.length();
                    }
                }
            }

            System.out.println(max);
        }
    }
}
